package com.dragondig.tetris;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.database.Cursor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class ScoreActivity extends Activity{
	public static final int ABOUT_ID = Menu.FIRST;
	private final int WC = ViewGroup.LayoutParams.WRAP_CONTENT;
	
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.score);
        Button bt = (Button)findViewById(R.id.back);
        bt.setOnClickListener(backMain);
        DataProvider dp = new DataProvider();
        Cursor cr = dp.query(null, null, null, null, null);
        String[] player = new String[10];
        float[] scores = new float[10];
        if(cr == null || cr.getCount() <= 0){
        	player = null;
        	scores = null;
        }else{
        	for(int i = 0;i < cr.getCount();i ++){
        		player[i] = cr.getString(1);
        		scores[i] = cr.getFloat(2);
        	}
        }
        TableLayout tl = (TableLayout)findViewById(R.id.scoreTable);
        TableRow tempTableRow = null;
        TextView tempTextView = null;
        if(player == null || scores == null){
        	tempTableRow = new TableRow(this);
        	tl.addView(tempTableRow,new TableLayout.LayoutParams(WC,WC));
        	tempTextView = new TextView(this);
        	tempTableRow.addView(tempTextView);
        	tempTextView.setText("This position is just for you!");
        }else{
        	for(int i = 0;i < player.length;i ++){
            	tempTableRow = new TableRow(this);
            	tl.addView(tempTableRow,new TableLayout.LayoutParams(WC,WC));
            	tempTextView = new TextView(this);
            	tempTableRow.addView(tempTextView);
            	tempTextView.setText(player[i]);
            	tempTextView = new TextView(this);
            	tempTableRow.addView(tempTextView);
            	tempTextView.setText((int) scores[i]);
        	}
        }
    }
    private Button.OnClickListener backMain = new Button.OnClickListener(){
    	public void onClick(View v){
    		ScoreActivity.this.finish();
    	}
    };

    @Override
    public boolean onCreateOptionsMenu(Menu menu){
    	super.onCreateOptionsMenu(menu);
    	menu.add(0,ABOUT_ID,0,"About...");
    	return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item){
    	openAboutDialog();
    	return super.onOptionsItemSelected(item);
    }
    
    private void openAboutDialog(){
    	new AlertDialog.Builder(this)
    	.setTitle("About")
    	.setPositiveButton("OK", new OnClickListener(){
    		public void onClick(DialogInterface df,int whichButton){}
    		})
    	.setMessage(R.string.about)
    	.show();
    }
}
